********************************************************************************
*
*	Paper:		Assessing COVID-19 vaccine hesitancy, barriers, and uptake in Sub-Saharan Africa
*	Program:	Create tables and graphs
*	Authors:	Markhof
*	Last ed.:	11-05-2023
*
********************************************************************************

* Define macros
global Clean = ""
global Do = ""
global Data = ""
global Figures = ""
global Tables = ""

use "$Clean/all_countries_vaccine.dta", clear
run "$Do/05_programs.do"

set scheme white_tableau
graph set window fontface "Arial Narrow"


********************************************************************************
* FIGURES
********************************************************************************

* Figure 1: Share of the population fully vaccinated over time
********************************************************************************

preserve
	use "$Clean/vax_over_time.dta", replace
	xtline people_fully_vaccinated_per_hund if date > td(01jan2021) & (inlist(location, ///
		"Sub-Saharan Africa", "World", "Low income", "Lower middle income", "Upper middle income") ///
		| inlist(location, "High income", "Burkina Faso", "Kenya", "Malawi", "Nigeria", "Uganda", "Tanzania")), ///
		overlay ytitle("Percent") xtitle("Date") tscale(extend) ///
		legend(order(1 3 6 7 9 10 2 11 5 4 8 12))  ///
		plot2opts(lcolor(orange%60) lpattern(shortdash)) plot5opts(lcolor(eltgreen%90) lpattern(shortdash)) plot4opts(lcolor(erose%60) lpattern(shortdash)) ///
		plot11opts(lcolor(purple%60) lpattern(shortdash)) plot8opts(lcolor(cranberry%70) lpattern(".-")) plot12opts(lcolor(black%70) lpattern(".-"))
	
	graph export "$Figures/Figure1_vaccinated.png", as(png) replace
	graph export "$Figures/Figure1_vaccinated.pdf", as(pdf) replace


restore

* Figure 2: Vaccine acceptance among the unvaccinated
********************************************************************************

preserve
	tab vaccine_ynu, gen(vaxxine_)
	collapse (mean) vaxxine_1 vaxxine_2 vaxxine_3 [pw=hh_weight], by (country has_vaccine)
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Burkina Faso" & has_vaccine==0, name(Burkina1, replace) ///
		markersize(3.5) colors(dkorange gs11 gs14) legend(order(1 "Willing" 2 "Unwilling" 3 "Unsure") ///
		cols(3) position(6) size(*1.5)) title("{fontface Merriweather Bold: a. Burkina Faso}" "(57.3%, 53.0% - 61.5%)", margin(small) size(medsmall))
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Kenya" & has_vaccine==0, name(Kenya1, replace) ///
		markersize(3.5) colors(dkorange gs11 gs14) title("{fontface Merriweather Bold: b. Kenya}" "(86.4%, 81.8% - 91.0%)", margin(small) size(medsmall))		
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Malawi" & has_vaccine==0, name(Malawi1, replace) ///
		markersize(3.5) colors(dkorange gs11 gs14) title("{fontface Merriweather Bold: c. Malawi}" "(59.1%, 53.5% - 64.7%)", margin(small) size(medsmall))
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Nigeria" & has_vaccine==0, name(Nigeria1, replace) markersize(3.5) ///
		colors(dkorange gs11 gs14) title("{fontface Merriweather Bold: d. Nigeria}" "(70.5%, 67.3% - 73.8%)", margin(small) size(medsmall))
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Tanzania" & has_vaccine==0, name(Tanzania1, replace) markersize(3.5) ///
		colors(dkorange gs11 gs14) title("{fontface Merriweather Bold: e. Tanzania}" "(56.5%, 53.1% - 59.8%)", margin(small) size(medsmall))
	waffle vaxxine_1 vaxxine_2 vaxxine_3 if country=="Uganda" & has_vaccine==0, name(Uganda1, replace) markersize(3.5) ///
		colors(dkorange gs11 gs14) title("{fontface Merriweather Bold: f. Uganda}" "(34.4%, 25.8% - 42.9%)", margin(small) size(medsmall))

		
	grc1leg Burkina1 Kenya1 Malawi1 Nigeria1 Tanzania1 Uganda1,  ///
		name(unvaccinated, replace) rows(2)
	
	graph export "$Figures/Figure2_acceptance.pdf", as(pdf) replace
	graph export "$Figures/Figure2_acceptance.svg", as(svg) replace

	
restore



* Figure 3: Reasons for pending vaccination despite being willing to get vaccinated
********************************************************************************


preserve

	foreach var of varlist reason_pending* {
		replace `var' = `var'*100
	}

		foreach c in Nigeria Tanzania Uganda {
			svy: mean reason_pending* if country=="`c'"
			est sto `c'1
		}
		
		recode reason_pending_inaccessible (. = 0) if country=="Malawi" & !mi(reason_pending_knowledge)
		
		local vars reason_pending_ineligible reason_pending_knowledge reason_pending_crowded ///
			reason_pending_toofar reason_pending_vaxlack reason_pending_registration reason_pending_work ///
			reason_pending_domestic reason_pending_religious reason_pending_medical ///
			reason_pending_waiting reason_pending_afraid reason_pending_inaccessible reason_pending_other 

			svy: mean `vars' if country=="Malawi"
			est sto Malawi1
			
		coefplot Malawi1, bylabel("{fontface Merriweather Bold: a. Malawi}") color(navy) ciopts(recast(rcap) color(navy%40)) || ///
				Nigeria1, bylabel("{fontface Merriweather Bold: b. Nigeria}") color(green) ciopts(recast(rcap) color(green%40)) || ///
				Tanzania1, bylabel("{fontface Merriweather Bold: c. Tanzania}") color(red) ciopts(recast(rcap) color(red%40)) || ///
				, byopts(rows(1) legend(off)) subtitle(, bcolor(white)) norecycle plotregion(lcolor(black) lwidth(vthin)) ///
				order(reason_pending_vaxlack reason_pending_work reason_pending_toofar reason_pending_knowledge *) xtitle("Percent", size(small))
		
		graph export "$Figures/Figure3_barriers.svg", replace
		graph export "$Figures/Figure3_barriers.pdf", replace

	
restore		


* Figure 4: Most trusted information source on COVID-19 vaccines by vaccine acceptance
********************************************************************************

preserve
	qui: tab source_trusted, gen(trustsource_)
	foreach var of varlist trustsource_* {
		local varlab : variable label `var'
		local varlab : subinstr local varlab "source_trusted==" ""
		la var `var' "`varlab'"
	}
	recode vaccine (0 = 1) (1 = 0)
	la def accept 1 "Hesitant" 0 " Willing", replace
	la val vaccine accept

	graph dot trustsource_1 trustsource_13 [aw=hh_weight] if !inlist(country, "Tanzania", "Kenya"), ///
		over(vaccine) over(country) legend(order(1 "Medical Professionals" 2 "Media")position(6) cols(2))		
		
	graph export "$Figures/Figure4_infobyhesitancy.svg", as(svg) replace
	graph export "$Figures/Figure4_infobyhesitancy.pdf", as(pdf) replace
	
restore


* Figure 5: Perceived vaccine acceptance within their community among the hesitant and willing
********************************************************************************

preserve
	tab vacc_willing_community if inrange(vacc_willing_community, 0, 10), gen(vax_com_)

	replace vaccine = vaccine * 10
	foreach country of numlist 1 3/6 {
		sum vaccine [aw = hh_weight] if country1==`country'
		local mean = r(mean)
		local rate = round(r(mean))
		
		sum vacc_willing_community [aw = hh_weight] if inrange(vacc_willing_community, 0, 10) & country1==`country' & vaccine==0
		local mean_hesitant = r(mean)
		sum vacc_willing_community [aw = hh_weight] if inrange(vacc_willing_community, 0, 10) & country1==`country' & vaccine==10
		local mean_willing = r(mean)
		

		twoway (hist vacc_willing_community if inrange(vacc_willing_community, 0, 10) & country1==`country' & vaccine==0, barwidth(0.9) percent ytitle("%") bin(31) start(0) color("139 0 0")) ///
				(hist vacc_willing_community if inrange(vacc_willing_community, 0, 10) & country1==`country' & vaccine==10, barwidth(0.9) percent ytitle("%") bin(31) start(0) color(navy)) ///
				(scatteri 0 `mean' 40 `mean', c(l) m(i) color(khaki)) ///
				(scatteri 0 `mean_hesitant' 40 `mean_hesitant', c(l) m(i) mc(red)) ///
				(scatteri 0 `mean_willing' 40 `mean_willing', c(l) m(i) color(navy)), ///	
				/*legend(off)*/ xtitle("") name(C`country', replace) title("{fontface Merriweather Bold:`: label country1 `country''}") ///
				legend(order(1 "Hesitant" 2 "Willing" 3 "Avg acceptance (national estimate)" 4 "Avg acceptance (hesitant)" 5 "Avg acceptance (willing)") cols(1) size(*0.7))
		
		forvalues v = 1/11 {
			if `=`v'-1' == `rate' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-1' | `=`v'-1' == `=`rate'+1' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-2' | `=`v'-1' == `=`rate'+2' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-3' | `=`v'-1' == `=`rate'+3' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-4' | `=`v'-1' == `=`rate'+4' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-5' | `=`v'-1' == `=`rate'+5' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-6' | `=`v'-1' == `=`rate'+6' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-7' | `=`v'-1' == `=`rate'+7' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}
			if `=`v'-1' == `=`rate'-8' | `=`v'-1' == `=`rate'+8' {
				gr_edit .plotregion1.plot1.EditCustomStyle , j(`v') style(area(shadestyle(color("179 48 36"))))	
			}	
		}
		
		forvalues v = 1/11 {
			if `=`v'-1' == `rate' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-1' | `=`v'-1' == `=`rate'+1' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-2' | `=`v'-1' == `=`rate'+2' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-3' | `=`v'-1' == `=`rate'+3' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-4' | `=`v'-1' == `=`rate'+4' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-5' | `=`v'-1' == `=`rate'+5' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-6' | `=`v'-1' == `=`rate'+6' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-7' | `=`v'-1' == `=`rate'+7' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}
			if `=`v'-1' == `=`rate'-8' | `=`v'-1' == `=`rate'+8' {
				gr_edit .plotregion1.plot2.EditCustomStyle , j(`v') style(area(shadestyle(color("57 116 168%30"))))	
			}	
		}

	}

	grc1leg C1 C3 C4 C5 C6, ring(0)

	gr_edit legend.xoffset = 55
	gr_edit legend.yoffset = 12


	graph export "$Figures/Figure5_perceptions.svg", as(svg) width(10in) replace
	graph export "$Figures/Figure5_perceptions.pdf", as(pdf) width(10in) replace

restore


* Figure 6: Decisionmaker about vaccine uptake among adult household members
********************************************************************************

graph pie country1 [pw=hh_weight] if country!="Kenya", over(decisionmaker) plabel(_all percent, format(%2.1f)) ///
	legend(order(1 "Each adult for themselves" 2 "All adults together" 3 "Household head" ///
	4 "Other household member") cols(1)  ring(0)) intensity(*0.5) line(lcolor(black) lwidth(thin)) ///
	by(country, note("", size(vsmall)))
	
	gr_edit legend.xoffset = 45
	gr_edit legend.yoffset = 25


graph export "$Figures/Figure6_decisionmaker.svg", as(svg) replace
graph export "$Figures/Figure6_decisionmaker.pdf", as(pdf) replace


********************************************************************************
* TABLES
********************************************************************************


* Table A1: Vaccine hesitancy
********************************************************************************
preserve
	gen overall 		= vaccine
	gen unvaccinated 	= vaccine if has_vaccine == 0
	gen men				= vaccine if male == 1
	gen women			= vaccine if male == 0
	gen urban1			= vaccine if urban == 1
	gen rural1			= vaccine if urban == 0
	
	la var overall "Overall"
	la var unvaccinated "Unvaccinated"
	la var men "Men"
	la var women "Women"
	la var urban1 "Urban"
	la var rural1 "Rural"
	
	foreach country of numlist 1/6 {
		count if !mi(vaccine) & country1==`country'
		local N_overall`country' = r(N)
		
		count if !mi(vaccine) & has_vaccine==0 & country1==`country'
		local N_unvaccinated`country' = r(N)
		
		count if !mi(vaccine) & male==1 & country1==`country'
		local N_male`country' = r(N)

		count if !mi(vaccine) & male==0 & country1==`country'
		local N_female`country' = r(N)
		
		count if !mi(vaccine) & urban==1 & country1==`country'
		local N_urban`country' = r(N)

		count if !mi(vaccine) & urban==0 & country1==`country'
		local N_rural`country' = r(N)

		
		if `country' == 6 {
			matrix N1 = (`N_overall1', `N_overall2', `N_overall3', `N_overall4', `N_overall5', `N_overall6' \ ///
				`N_unvaccinated1', `N_unvaccinated2', `N_unvaccinated3', `N_unvaccinated4', `N_unvaccinated5', `N_unvaccinated6' \ ///
				`N_male1', `N_male2', `N_male3', `N_male4', `N_male5', `N_male6' \ ///
				`N_female1', `N_female2', `N_female3', `N_female4', `N_female5', `N_female6' \ ///
				`N_urban1', `N_urban2', `N_urban3', `N_urban4', `N_urban5', `N_urban6' \ ///
				`N_rural1', `N_rural2', `N_rural3', `N_rural4', `N_rural5', `N_rural6')
		}

	}

	mytable_multvar overall unvaccinated men women urban1 rural1, save($Tables/Table_A1.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) no_overlap(1) ///
		title("Table A1: Estimated vaccine acceptance")		
		
	svyset [pw = hh_weight], strata(strata) singleunit(centered)
	local i = 0
	foreach var in overall unvaccinated men women urban1 rural1 {
		local i = `i'+1
		svy: mean `var' if country=="Kenya"
		
		if `i'==1 {
			matrix table = r(table)
		}
		
		else {
			matrix table = table, r(table)
		}
		
	}

	matrix ken 		= J(6, 1, .)
	matrix lb_ken 	= J(6, 1, .)
	matrix ub_ken 	= J(6, 1, .)

	foreach num of numlist 1/6 {
		matrix ken[`num', 1] = table[1, `num']
		matrix lb_ken[`num', 1] = table[5, `num']
		matrix ub_ken[`num', 1] = table[6, `num']
	}

	matrix ken = ken*100

	local i = 0
	foreach x of numlist 5(2)15 {
		local i = `i'+1
		
		local k : display %2.1f lb_ken[`i',1]*100
		local l : display %2.1f ub_ken[`i',1]*100
		
		local m = "(`k' to `l')"

		putexcel C`x' = matrix(ken[`i',1]), nformat(#0.0) hcenter
		putexcel C`=`x'+1' = "`m'", hcenter
	}

		
		
	putexcel A17 = "N (overall)", left italic font("Times New Roman", 8)
	putexcel A18 = "N (unvaccinated)", left italic	font("Times New Roman", 8)
	putexcel A19 = "N (male)", left italic	font("Times New Roman", 8)
	putexcel A20 = "N (female)", left italic	font("Times New Roman", 8)
	putexcel A21 = "N (urban)", left italic	font("Times New Roman", 8)
	putexcel A22 = "N (rural)", left italic	font("Times New Roman", 8)
	
	putexcel A18, unmerge
	putexcel B17 = matrix(N1), hcenter nformat(#,##0) font("Times New Roman", 8)
	putexcel A18:G18, border(top, none)
	putexcel A23:G23 = "Note: The table compares estimated acceptance rates for COVID-19 vaccines. The breakdown for different demographics includes those already vaccinated. All values in percent. 95% confidence intervals in parentheses.", merge txtwrap font("Times New Roman", 10)	border(top, medium)
	putexcel A3:G23, fpattern(solid, white)


* t-tests
	* need to reassign some singleunit strata in KEN for standard error
	recode strata (59/95 = 59) if country1==2

	* Men vs. women
	foreach c of numlist 1/6 {
		di "************ `: label country1 `c'' ***************"
		svy: reg vaccine male if country1==`c'
	}

	foreach c of numlist 1/6 {
		di "************ `: label country1 `c'' ***************"
		svy: reg vaccine urban if country1==`c'
	}	
	
restore

* Table A2: Reasons for not having been vaccinated yet despite being willing to get vaccinated
********************************************************************************

mytable_multvar reason_pending_*, save($Tables/Table_A2.xlsx) ccodes(3,4,5,6) lvl_num(4) ///
	title("Table A2: Reasons for pending vaccination") colheader("Reasons") ///
	note("Note: Reasons for pending vaccination despite being willing to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")

* One answer option missing in Malawi, need to add "by hand"
matrix mwi = J(14, 1, .)
matrix lb_mwi = J(14, 1, .)
matrix ub_mwi = J(14, 1, .)

local vars reason_pending_ineligible reason_pending_knowledge reason_pending_crowded ///
	reason_pending_toofar reason_pending_vaxlack reason_pending_registration reason_pending_work ///
	reason_pending_domestic reason_pending_religious reason_pending_medical ///
	reason_pending_waiting reason_pending_afraid reason_pending_other 

svy: mean `vars' if country=="Malawi"
local N = e(N)

foreach num of numlist 1/3 {
	matrix mwi[`num', 1] = r(table)[1, `num']
	matrix lb_mwi[`num', 1] = r(table)[5, `num']
	matrix ub_mwi[`num', 1] = r(table)[6, `num']
}
foreach num of numlist 5/14 {
	matrix mwi[`num', 1] = r(table)[1, `=`num'-1']
	matrix lb_mwi[`num', 1] = r(table)[5, `=`num'-1']
	matrix ub_mwi[`num', 1] = r(table)[6, `=`num'-1']
}

matrix mwi = mwi*100

local i = 0
foreach x of numlist 5(2)31 {
	local i = `i'+1
	
	local k : display %2.1f lb_mwi[`i',1]*100
	local l : display %2.1f ub_mwi[`i',1]*100
	
	if `i' != 4 {
		local m = "(`k' to `l')"
	}

	else {
		local m = " "
	}

	putexcel B`x' = matrix(mwi[`i',1]), nformat(#0.0) hcenter
	putexcel B`=`x'+1' = "`m'", hcenter
	putexcel B33 = `N'
	}

	
	
	
	
* Table A3: Reasons for not having been vaccinated yet despite being willing 
*			to get vaccinated by urban/rural
********************************************************************************
mytable_multvar reason_pending*, save($Tables/Table_A3.xlsx) ccodes(3,4,5,6) lvl_num(4) ///
	title("Table A3: Reasons for pending vaccination by residence") colheader("Reasons") over(urban) ///
	note("Note: Reasons for pending vaccination despite being willing to get vaccinated, by urban/rural. All values in percent. 95% confidence intervals in parentheses.")
	
* One answer option missing in Malawi, need to add "by hand"
matrix mwi = J(14, 2, .)
matrix lb_mwi = J(14, 2, .)
matrix ub_mwi = J(14, 2, .)

local vars reason_pending_ineligible reason_pending_knowledge reason_pending_crowded ///
	reason_pending_toofar reason_pending_vaxlack reason_pending_registration reason_pending_work ///
	reason_pending_domestic reason_pending_religious reason_pending_medical ///
	reason_pending_waiting reason_pending_afraid reason_pending_other 

svy: mean `vars' if country=="Malawi", over(urban)

local i = 0
foreach num of numlist 1(2)25 {
	local i = `i' + 1
	
	if `i' == 4 {
		local i = `i'+1
	}
	
	matrix mwi[`i', 1] = r(table)[1, `num'..`=`num'+1']
	matrix lb_mwi[`i', 1] = r(table)[5, `num'..`=`num'+1']
	matrix ub_mwi[`i', 1] = r(table)[6, `num'..`=`num'+1']
}


count if !mi(reason_pending_ineligible) & male==0 & country1==3
matrix N1 = r(N)

count if !mi(reason_pending_ineligible) & male==1 & country1==3
matrix N1 = N1, r(N)				


matrix mwi = mwi*100

local i = 0
foreach x of numlist 5(2)31 {
	local i = `i'+1
	
	local k0 : display %2.1f lb_mwi[`i',1]*100
	local l0 : display %2.1f ub_mwi[`i',1]*100
	local k1 : display %2.1f lb_mwi[`i',2]*100
	local l1 : display %2.1f ub_mwi[`i',2]*100

	
	if `i' != 4 {
		local m0 = "(`k0' to `l0')"
		local m1 = "(`k1' to `l1')"		
	}

	else {
		local m = " "
	}

	putexcel B`x' = matrix(mwi[`i',1..2]), nformat(#0.0) hcenter
	putexcel B`=`x'+1' = "`m0'", hcenter
	putexcel C`=`x'+1' = "`m1'", hcenter
	putexcel B33 = matrix(N1)
}
	

* Table A4: Reasons for not having been vaccinated yet despite being willing 
*			to get vaccinated by gender
********************************************************************************
mytable_multvar reason_pending*, save($Tables/Table_A4.xlsx) ccodes(3,4,5,6) lvl_num(4) ///
	title("Table A4: Reasons for pending vaccination by gender") colheader("Reasons") over(male) ///
	note("Note: Reasons for pending vaccination despite being willing to get vaccinated, by gender. All values in percent. 95% confidence intervals in parentheses.")
	
* One answer option missing in Malawi, need to add "by hand"
matrix mwi = J(14, 2, .)
matrix lb_mwi = J(14, 2, .)
matrix ub_mwi = J(14, 2, .)

local vars reason_pending_ineligible reason_pending_knowledge reason_pending_crowded ///
	reason_pending_toofar reason_pending_vaxlack reason_pending_registration reason_pending_work ///
	reason_pending_domestic reason_pending_religious reason_pending_medical ///
	reason_pending_waiting reason_pending_afraid reason_pending_other 

svy: mean `vars' if country=="Malawi", over(male)

local i = 0
foreach num of numlist 1(2)25 {
	local i = `i' + 1
	
	if `i' == 4 {
		local i = `i'+1
	}
	
	matrix mwi[`i', 1] = r(table)[1, `num'..`=`num'+1']
	matrix lb_mwi[`i', 1] = r(table)[5, `num'..`=`num'+1']
	matrix ub_mwi[`i', 1] = r(table)[6, `num'..`=`num'+1']
}


count if !mi(reason_pending_ineligible) & urban==0 & country1==3
matrix N1 = r(N)

count if !mi(reason_pending_ineligible) & urban==1 & country1==3
matrix N1 = N1, r(N)				


matrix mwi = mwi*100

local i = 0
foreach x of numlist 5(2)31 {
	local i = `i'+1
	
	local k0 : display %2.1f lb_mwi[`i',1]*100
	local l0 : display %2.1f ub_mwi[`i',1]*100
	local k1 : display %2.1f lb_mwi[`i',2]*100
	local l1 : display %2.1f ub_mwi[`i',2]*100

	
	if `i' != 4 {
		local m0 = "(`k0' to `l0')"
		local m1 = "(`k1' to `l1')"		
	}

	else {
		local m = " "
	}

	putexcel B`x' = matrix(mwi[`i',1..2]), nformat(#0.0) hcenter
	putexcel B`=`x'+1' = "`m0'", hcenter
	putexcel C`=`x'+1' = "`m1'", hcenter
	putexcel B33 = matrix(N1)
}	
	
* Table A5: Barriers of access (Kenya)
********************************************************************************
/*preserve
svyset [pw = hh_weight], strata(strata) singleunit(centered) // single-stage design for KEN
	mytable_multvar barriers_*, save($Tables/Table_A5.xlsx) ccodes(2) lvl_num(1) ///
		title("Table A5: Barriers of access (Kenya)") colheader("Barriers") ///
		note("Note: Main anticipated difficulties to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")

	mytable_multvar barriers_*, save($Tables/Table_A5.xlsx) ccodes(2) lvl_num(1) ///
		title("Table A5: Barriers of access (Kenya)") colheader("Barriers") over(urban) ///
		note("Note: Main anticipated difficulties to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")
		
	mytable_multvar barriers_*, save($Tables/Table_A5.xlsx) ccodes(2) lvl_num(1) ///
		title("Table A5: Barriers of access (Kenya)") colheader("Barriers") over(male) ///
		note("Note: Main anticipated difficulties to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")	
restore	
	
*/

* Table A6: Vaccination location
********************************************************************************
preserve
	replace vacc_location_other = vacc_location_mobileteam if country=="Kenya"
	drop vacc_location_mobileteam
	
	recode vacc_location_other (0 = 1) if inlist(1, vacc_location_home, vacc_location_headoffice, vacc_location_govtoffice) & country1==6
	drop vacc_location_home vacc_location_headoffice vacc_location_govtoffice
	
	mytable_multvar vacc_location_*, save($Tables/Table_A6.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) ///
		title("Table A6: Vaccination location") colheader("Location") ///
		note("Note: Vaccination location. All values in percent. 95% confidence intervals in parentheses.")
		
	* Several answer option missing in Kenya, need to add "by hand"
	svyset [pw=hh_weight], strata(strata) singleunit(centered)
	matrix ken = J(9, 1, .)
	matrix lb_ken = J(9, 1, .)
	matrix ub_ken = J(9, 1, .)

	local vars  vacc_location_hospital vacc_location_clinic vacc_location_hcenter ///
		vacc_location_pharmacy vacc_location_seniorhome vacc_location_masssite vacc_location_other

	svy: mean `vars' if country=="Kenya"
	local N = e(N)

	foreach num of numlist 1/6 {
		matrix ken[`num', 1] = r(table)[1, `num']
		matrix lb_ken[`num', 1] = r(table)[5, `num']
		matrix ub_ken[`num', 1] = r(table)[6, `num']
	}
	foreach num of numlist 9 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-2']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-2']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-2']
	}

	matrix ken = ken*100

	local i = 0
	foreach x of numlist 5(2)21 {
		local i = `i'+1
		
		local k : display %2.1f lb_ken[`i',1]*100
		local l : display %2.1f ub_ken[`i',1]*100
		
		if `i' != 7 & `i' != 8 {
			local m = "(`k' to `l')"
		}

		else  {
			local m = " "
		}

		putexcel C`x' = matrix(ken[`i',1]), nformat(#0.0) hcenter
		putexcel C`=`x'+1' = "`m'", hcenter
		putexcel C23 = `N'
		}
			
restore

* Table A7: Reasons for getting vaccinated
********************************************************************************
preserve
	recode vacc_reason_other (0 = 1) if vacc_reason_ethics==1 & country=="Kenya"
	recode vacc_reason_other (0 = 1) if vacc_reason_pressure==1 & country=="Kenya"
	drop vacc_reason_ethics vacc_reason_pressure
	
	mytable_multvar vacc_reason_*, save($Tables/Table_A7.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) ///
		title("Table A7: Reasons for vaccination") colheader("Reason") ///
		note("Note: Reasons for getting vaccinated. 'Other' mainly comprises of people citing ethical reasons as their motivation in Kenya (29.2%).  All values in percent. 95% confidence intervals in parentheses.")

	* Several answer option missing in Kenya, need to add "by hand"
	svyset [pw=hh_weight], strata(strata) singleunit(centered)
	matrix ken = J(11, 1, .)
	matrix lb_ken = J(11, 1, .)
	matrix ub_ken = J(11, 1, .)

	local vars vacc_reason_ownhealth vacc_reason_otherhealth vacc_reason_school ///
		 vacc_reason_employer vacc_reason_social vacc_reason_other ///
		

	svy: mean `vars' if country=="Kenya"
	local N = e(N)

	foreach num of numlist 1/2 {
		matrix ken[`num', 1] = r(table)[1, `num']
		matrix lb_ken[`num', 1] = r(table)[5, `num']
		matrix ub_ken[`num', 1] = r(table)[6, `num']
	}
	foreach num of numlist 4/5 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-1']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-1']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-1']
	}
	foreach num of numlist 7 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-2']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-2']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-2']
	}
	foreach num of numlist 11 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-5']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-5']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-5']
	}

	matrix ken = ken*100

	local i = 0
	foreach x of numlist 5(2)25 {
		local i = `i'+1
		
		local k : display %2.1f lb_ken[`i',1]*100
		local l : display %2.1f ub_ken[`i',1]*100
		
		if !inlist(`i', 3,6,8,9,10) {
			local m = "(`k' to `l')"
		}

		else  {
			local m = " "
		}

		putexcel C`x' = matrix(ken[`i',1]), nformat(#0.0) hcenter
		putexcel C`=`x'+1' = "`m'", hcenter
		putexcel C27 = `N'
		}		
		
restore

* Table A8: Reasons for hesitancy
********************************************************************************
preserve
	replace reason_hesitant_unsafe = 0 if mi(reason_hesitant_unsafe) & vaccine==0
	mytable_multvar reason_hesitant_*, save($Tables/Table_A8.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) ///
		title("Table A8: Reasons for hesitancy") colheader("Reason") ///
		note("Note: Reasons for being hesitant to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")
		
	* Two answer options missing in Malawi, need to add "by hand"
	matrix mwi = J(17, 1, .)
	matrix lb_mwi = J(17, 1, .)
	matrix ub_mwi = J(17, 1, .)

	local vars reason_hesitant_ineffective reason_hesitant_sideeffects reason_hesitant_norisk ///
		reason_hesitant_notrust reason_hesitant_religion reason_hesitant_getinfected ///
		reason_hesitant_toofar reason_hesitant_toolong reason_hesitant_inferiorvax ///
		reason_hesitant_novaxchoice reason_hesitant_noprio reason_hesitant_covidnoexist ///
		reason_hesitant_medical reason_hesitant_other

	svy: mean `vars' if country=="Malawi"
	local N = e(N)

	foreach num of numlist 1/2 {
		matrix mwi[`num', 1] = r(table)[1, `num']
		matrix lb_mwi[`num', 1] = r(table)[5, `num']
		matrix ub_mwi[`num', 1] = r(table)[6, `num']
	}
	foreach num of numlist 4/12 {
		matrix mwi[`num', 1] = r(table)[1, `=`num'-1']
		matrix lb_mwi[`num', 1] = r(table)[5, `=`num'-1']
		matrix ub_mwi[`num', 1] = r(table)[6, `=`num'-1']
	}
	foreach num of numlist 14/16 {
		matrix mwi[`num', 1] = r(table)[1, `=`num'-2']
		matrix lb_mwi[`num', 1] = r(table)[5, `=`num'-2']
		matrix ub_mwi[`num', 1] = r(table)[6, `=`num'-2']
	}

	matrix mwi = mwi*100

	local i = 0
	foreach x of numlist 5(2)37 {
		local i = `i'+1
		
		local k : display %2.1f lb_mwi[`i',1]*100
		local l : display %2.1f ub_mwi[`i',1]*100
		
		if `i' != 3 & `i' != 13 & `i' != 17 {
			local m = "(`k' to `l')"
		}

		else {
			local m = " "
		}

		putexcel D`x' = matrix(mwi[`i',1]), nformat(#0.0) hcenter
		putexcel D`=`x'+1' = "`m'", hcenter
		putexcel D39 = `N'
		}

		
	* One answer options missing in Uganda, need to add "by hand"
	matrix uga = J(17, 1, .)
	matrix lb_uga = J(17, 1, .)
	matrix ub_uga = J(17, 1, .)

	local vars reason_hesitant_ineffective reason_hesitant_sideeffects reason_hesitant_hadcovid ///
	reason_hesitant_norisk reason_hesitant_notrust reason_hesitant_religion reason_hesitant_getinfected ///
	reason_hesitant_toofar reason_hesitant_toolong reason_hesitant_inferiorvax reason_hesitant_novaxchoice ///
	reason_hesitant_noprio reason_hesitant_covidnoexist reason_hesitant_medical reason_hesitant_other

	svy: mean `vars' if country=="Uganda"
	local N = e(N)

	foreach num of numlist 1/14 {
		matrix uga[`num', 1] = r(table)[1, `num']
		matrix lb_uga[`num', 1] = r(table)[5, `num']
		matrix ub_uga[`num', 1] = r(table)[6, `num']
	}
	foreach num of numlist 16 {
		matrix uga[`num', 1] = r(table)[1, `=`num'-1']
		matrix lb_uga[`num', 1] = r(table)[5, `=`num'-1']
		matrix ub_uga[`num', 1] = r(table)[6, `=`num'-1']
	}

	matrix uga = uga*100

	local i = 0
	foreach x of numlist 5(2)37 {
		local i = `i'+1
		
		local k : display %2.1f lb_uga[`i',1]*100
		local l : display %2.1f ub_uga[`i',1]*100
		
		if `i' != 15 & `i' != 17 {
			local m = "(`k' to `l')"
		}

		else {
			local m = " "
		}

		putexcel G`x' = matrix(uga[`i',1]), nformat(#0.0) hcenter
		putexcel G`=`x'+1' = "`m'", hcenter
		putexcel G39 = `N'
		}
	
		
	* Several answer option missing in Kenya, need to add "by hand"
	svyset [pw=hh_weight], strata(strata) singleunit(centered)
	matrix ken = J(17, 1, .)
	matrix lb_ken = J(17, 1, .)
	matrix ub_ken = J(17, 1, .)

	
	
	local vars reason_hesitant_ineffective reason_hesitant_sideeffects ///
		reason_hesitant_norisk reason_hesitant_notrust reason_hesitant_religion ///
		reason_hesitant_getinfected reason_hesitant_toofar reason_hesitant_toolong ///
		reason_hesitant_other reason_hesitant_unsafe
		

	svy: mean `vars' if country=="Kenya"
	local N = e(N)

	foreach num of numlist 1/2 {
		matrix ken[`num', 1] = r(table)[1, `num']
		matrix lb_ken[`num', 1] = r(table)[5, `num']
		matrix ub_ken[`num', 1] = r(table)[6, `num']
	}
	foreach num of numlist 4/9 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-1']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-1']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-1']
	}
	foreach num of numlist 16/17 {
		matrix ken[`num', 1] = r(table)[1, `=`num'-7']
		matrix lb_ken[`num', 1] = r(table)[5, `=`num'-7']
		matrix ub_ken[`num', 1] = r(table)[6, `=`num'-7']
	}

	matrix ken = ken*100

	local i = 0
	foreach x of numlist 5(2)37 {
		local i = `i'+1
		
		local k : display %2.1f lb_ken[`i',1]*100
		local l : display %2.1f ub_ken[`i',1]*100
		
		if !inlist(`i', 3,10,11,12,13,14,15) {
			local m = "(`k' to `l')"
		}

		else  {
			local m = " "
		}

		putexcel C`x' = matrix(ken[`i',1]), nformat(#0.0) hcenter
		putexcel C`=`x'+1' = "`m'", hcenter
		putexcel C39 = `N'
		putexcel B37 = " "
		putexcel D37 = " "
		putexcel E37 = " "
		putexcel F37 = " "
		}		
	
restore


* Table A9: Most trusted information sources
********************************************************************************
preserve
	qui: tab source_trusted, gen(trustsource_)
			foreach var of varlist trustsource_* {
				local varlab : variable label `var'
				local varlab : subinstr local varlab "source_trusted==" ""
				la var `var' "`varlab'"
			}


	mytable_multvar trustsource_*, save($Tables/Table_A9.xlsx) ccodes(1,3,4,6) lvl_num(4) ///
		title("Table A9: Most trusted information sources") colheader("Information Source") ///
		note("Note: Most trusted information sources. All values in percent. 95% confidence intervals in parentheses.")
restore


* Table A10: Most trusted information sources, by vaccine acceptance
********************************************************************************
preserve
	qui: tab source_trusted, gen(trustsource_)
			foreach var of varlist trustsource_* {
				local varlab : variable label `var'
				local varlab : subinstr local varlab "source_trusted==" ""
				la var `var' "`varlab'"
			}

la def vaccine 0 "Hesitant" 1 "Willing"
la val vaccine vaccine

	mytable_multvar trustsource_*, save($Tables/Table_A10.xlsx) ccodes(1,3,4,6) lvl_num(4) ///
		title("Table A10: Most trusted information sources by vaccine acceptance") colheader("Information Source") over(vaccine) ///
		note("Note: Most trusted information sources by vaccine acceptance. All values in percent. 95% confidence intervals in parentheses.")
restore



* Table A11: Vaccine ambassadors for the hesitant
********************************************************************************
egen any_ambassador = rowmax(ambassador*)
mean any_ambassador [pw = hh_weight], over(country1)
la var any_ambassador "At least some ambassador"

mytable_multvar any_ambassador ambassador*, save($Tables/Table_A11.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) ///
	title("Table A11: Vaccine ambassadors") colheader("Ambassador") ///
	note("Note: People whose recommendation would make the hesitant more likely to get vaccinated. All values in percent. 95% confidence intervals in parentheses.")

putexcel A6:G6 , border(bottom, double)

* Need to do Kenya manually due to different survey design
preserve
	svyset [pw=hh_weight], strata(strata) singleunit(centered)
	
	svy: mean any_ambassador ambassador* if country=="Kenya"

	matrix ken = J(9, 1, .)
	matrix lb_ken = J(9, 1, .)
	matrix ub_ken = J(9, 1, .)

	
	foreach num of numlist 1/9 {
		matrix ken[`num', 1] = r(table)[1, `num']
		matrix lb_ken[`num', 1] = r(table)[5, `num']
		matrix ub_ken[`num', 1] = r(table)[6, `num']
	}

	matrix ken = ken*100

	local i = 0
	foreach x of numlist 5(2)21 {
		local i = `i'+1
		
		local k : display %2.1f lb_ken[`i',1]*100
		local l : display %2.1f ub_ken[`i',1]*100
		
		local m = "(`k' to `l')"

		putexcel C`x' = matrix(ken[`i',1]), nformat(#0.0) hcenter
		putexcel C`=`x'+1' = "`m'", hcenter
	}
	
restore	


* Table A12: Information channels through which most trusted info is obtained
********************************************************************************

mytable_multvar channel*, save($Tables/Table_A12.xlsx) ccodes(1,3,4,6) lvl_num(4) ///
	title("Table A12: Information channels") colheader("Channel") ///
	note("Note: Information channels through which the most trusted information on COVID-19 was obtained. All values in percent. 95% confidence intervals in parentheses.")




* Table A13: Perceived acceptance of COVID-19 vaccines in the community,
*			overall and by vaccine acceptance 
********************************************************************************

preserve
	replace vaccine 	= vaccine
	gen vax_comperc 	= vacc_willing_community*0.1 if vacc_willing_community!=99
	gen vax_comperc_hes = vax_comperc if vaccine==0
	gen vax_comperc_acc = vax_comperc if vaccine==1
	gen answered = vacc_willing_community!=99 & !mi(vacc_willing_community)
	
	la var vax_comperc "Average perceived acceptance"
	la var vax_comperc_hes "Perceived acceptance (hesitant)"
	la var vax_comperc_acc "Perceived acceptance (willing)"
	la var vaccine "Estimated national acceptance"
	
	foreach country of numlist 1 3/6 {
		count if !mi(vaccine) & country1==`country'
		local N_national`country' = r(N)
		
		count if !mi(vax_comperc) & country1==`country'
		local N_perceived`country' = r(N)
		
		count if !mi(vax_comperc_hes) & country1==`country'
		local N_hes`country' = r(N)

		count if !mi(vax_comperc_acc) & country1==`country'
		local N_acc`country' = r(N)
		
		if `country' == 6 {
			matrix N1 = (`N_national1', `N_national3', `N_national4', `N_national5', `N_national6' \ `N_perceived1', `N_perceived3', `N_perceived4', `N_perceived5', `N_perceived6' \ `N_hes1', `N_hes3', `N_hes4', `N_hes5', `N_hes6' \ `N_acc1', `N_acc3', `N_acc4', `N_acc5', `N_acc6')
		}

	}

	mytable_multvar vaccine vax_comperc*, save($Tables/Table_A13.xlsx) ccodes(1,3,4,5,6) lvl_num(5) no_overlap(1) ///
		title("Table A13: Perceived vaccine acceptance") colheader("Estimate") ///
		/*note("Note: Average perceived acceptance of COVID-19 vaccines in one's community. All values in percent. 95% confidence intervals in parentheses.")*/
		
	putexcel A13 = "N (national)", left italic font("Times New Roman", 8)
	putexcel A14 = "N (perceived)", left italic	font("Times New Roman", 8)
	putexcel A15 = "N (hesitant)", left italic	font("Times New Roman", 8)
	putexcel A16 = "N (willing)", left italic	font("Times New Roman", 8)
	putexcel A14, unmerge
	putexcel B13 = matrix(N1), hcenter nformat(#,##0) font("Times New Roman", 8)
	putexcel A14:F14, border(top, none)
	putexcel A17:F17 = "Note: Average perceived acceptance of COVID-19 vaccines in one's community. All values in percent. 95% confidence intervals in parentheses.", merge txtwrap font("Times New Roman", 10)	border(top, medium)
	putexcel A3:F17, fpattern(solid, white)
restore


* t-Test of mean differences between perception and actual value (no table)
********************************************************************************
preserve
	recode vacc_willing_community (99 = .)
	replace vaccine = vaccine * 10
	foreach c of numlist 1 3/6 {
		
		di "############## `: label country1 `c'' #################"
		
		qui: svy: regress c.vaccine if country1==`c'
		est sto m1

		qui: svy: regress c.vacc_willing_community if country1==`c'
		est sto m2

		suest m1 m2

		test [m1 = m2], cons
	}
restore




* Table A14: Decisionmakers within the household
********************************************************************************

preserve
	qui: tab decisionmaker, gen(hh_decisionmaker_)
			foreach var of varlist hh_decisionmaker_* {
				local varlab : variable label `var'
				local varlab : subinstr local varlab "decisionmaker==" ""
				la var `var' "`varlab'"
			}
	

	mytable_multvar hh_decisionmaker_*, save($Tables/Table_A14.xlsx) ccodes(1,3,4,5,6) lvl_num(5) ///
		title("Table A14: Decisionmaker within the household") colheader("Decisionmaker") ///
		note("Note: Decisionmaker within the household about vaccine uptake of adult household members. All values in percent. 95% confidence intervals in parentheses.")

restore



* Table A15: Likelihood to encourage others to get vaccinated
********************************************************************************

preserve
	qui: tab vaccine_encourage, gen(vax_encourage)
			foreach var of varlist vax_encourage* {
				local varlab : variable label `var'
				local varlab : subinstr local varlab "vaccine_encourage==" ""
				la var `var' "`varlab'"
			}

	mytable_multvar vax_encourage*, save($Tables/Table_A15.xlsx) ccodes(1,3,4,5,6) lvl_num(5) ///
		title("Table A15: Likelihood to encourage others") colheader("Likelihood") ///
		note("Note: Likelihood to encourage others to get vaccinated among those already vaccinated. All values in percent. 95% confidence intervals in parentheses.")

restore



* Table A16: Phone survey respondents vs. general population
********************************************************************************

use "$Clean/baseline_indiv.dta", clear

replace hh_weight = f2fweight if phone_sample == 0

qui: tab educ_degree, gen(educ_)
		foreach var of varlist educ_? {
			local varlab : variable label `var'
			local varlab : subinstr local varlab "educ_degree==" ""
			la var `var' "`varlab'"
		}
		
drop if age < 15	// only compare adult population

encode country, gen(country1)

svyset [pw = hh_weight], singleunit(centered)


replace age = age / 100

foreach num of numlist 1/4 {
	replace educ_`num' = 0 if country=="Tanzania" // this is so that the code runs, will correct below
	replace educ_`num' = 0 if country=="Kenya" // this is so that the code runs, will correct below
	
}

mytable_multvar age female educ_?, save($Tables/Table_A16.xlsx) ccodes(1,2,3,4,5,6) lvl_num(6) ///
	title("Table A16: General Adult Population vs. Phone Survey Sample") colheader("Characteristic") over(phone_sample) ///
	note("Note: Comparison of individual characteristics between general adult population (from nationally representative, face-to-face pre-COVID-19 survey) and phone survey respondents. Data for education not available in Kenya and Tanzania.")
	
	foreach r of numlist 9(2)15 {
		putexcel D`r' = "-"
		putexcel E`r' = "-"		
		putexcel J`r' = "-"
		putexcel K`r' = "-"
	}
	

foreach x of numlist 1 3 4 5 6 {
	sum age female educ_? [aw = hh_weight] if country1==`x' & phone_sample == 1
}

foreach x of numlist 1 3 4 5 6 {
	sum age female educ_? [aw = hh_weight] if country1==`x' & phone_sample == 0
}